//
//  JShopCarViewController.m
//  HeadIndustry
//
//  Created by jun on 2017/8/24.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JShopCarViewController.h"

#import "JProductDetailController.h"
#import "JCustomerAddressController.h"
#import "JShopCarHeaderView.h" // headerView
#import "JShopCarTableCell.h" // cell
#import "JLikeView.h" // 猜你喜欢
#import "JPayController.h" // 结算页面

#define  TAG_CartEmptyView 100
#define HeaderViewHeight 45

@interface JShopCarViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic , strong) JLikeView *foorerView;

@property (nonatomic , strong) NSMutableArray *dataArray;
@property (nonatomic , strong) UITableView *myTableView;
@property (strong,nonatomic)NSMutableArray *selectedArray;
@property (strong,nonatomic)NSMutableArray *changeArray;

@property (nonatomic , strong) UILabel *totlePriceLabel;
@property (nonatomic , strong) UIButton *allSellectedButton;
@property (nonatomic , strong) UIButton *allCountButton;

@property (nonatomic, strong) JYouLikeModel *youLikeModel;  // 猜你喜欢参数 model
@property (nonatomic, strong) NSMutableString *youLikeStr;

@property (nonatomic , strong) NSMutableString *productSelIds;

@end

@implementation JShopCarViewController

- (JYouLikeModel *)youLikeModel
{
    if (!_youLikeModel) {
        _youLikeModel = [JYouLikeModel new];
        _youLikeModel.pageIndex = 1;
        _youLikeModel.pageSize = 10;
        
    }
    return _youLikeModel;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.selectedArray.count > 0) {
        for (JShopCarShopModel *model in self.selectedArray) {
            model.isSel = NO;//这个其实有点多余,提交订单后的数据源不会包含这些,保险起见,加上了
        }
        [self.selectedArray removeAllObjects];
    }
    
    //初始化显示状态
    _allSellectedButton.selected = NO;
    _totlePriceLabel.attributedText = [self DWQSetString:@"￥0.00"];
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [super InitUI];
    
    self.titleLabel.text = @"购物车";
    self.leftButton.hidden = YES;
    self.rightButton.hidden = YES;

    _dataArray = [NSMutableArray array];
    _selectedArray = [NSMutableArray array];
    _changeArray = [NSMutableArray array];
    
    _productSelIds = [NSMutableString string];
//    [self setupCartEmptyView];
    
//    [self setupCustomBottomView];
    
//    [self loadData];
}


- (void)setupCartEmptyView {
    //默认视图背景
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame))];
    backgroundView.tag = TAG_CartEmptyView;
    [self.contentView addSubview:backgroundView];
    
    //默认图片
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shopcar_ematy"]];
    img.center = CGPointMake(kWindowW/2.0, kWindowH/2.0 - 120);
    img.bounds = CGRectMake(0, 0, kWindowW - 100, 100);
    [backgroundView addSubview:img];
    
    UILabel *warnLabel = [[UILabel alloc]init];
    warnLabel.center = CGPointMake(kWindowW/2.0, kWindowH/2.0 - 10);
    warnLabel.bounds = CGRectMake(0, 0, kWindowW, 60);
    warnLabel.textAlignment = NSTextAlignmentCenter;
    warnLabel.text = @"购物车还空着的，\n去挑几件中意的商品吧！";
    warnLabel.numberOfLines = 0;
    warnLabel.font = [UIFont systemFontOfSize:16];
    warnLabel.textColor = kRGBColor(150, 150 , 150);
    [backgroundView addSubview:warnLabel];
}

#pragma mark -- 购物车有商品时的视图
- (void)setupCartView {
    //创建底部视图
    [self setupCustomBottomView];
    
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame) - 49 - 49) style:UITableViewStyleGrouped];
    
    table.delegate = self;
    table.dataSource = self;
    
    table.rowHeight = 100;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:table];
    self.myTableView = table;
    
    [table registerClass:[JShopCarHeaderView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([JShopCarHeaderView class])];
    
    _foorerView = [[JLikeView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 0) isHeadView:YES];
    table.tableFooterView = _foorerView;
    
    [table registerClass:[JShopCarTableCell class] forCellReuseIdentifier:NSStringFromClass([JShopCarTableCell class])];
}

#pragma mark --- UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    JShopCarShopModel *model = [self.dataArray objectAtIndex:section];
    return model.goodsArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    JShopCarHeaderView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([JShopCarHeaderView class])];
    headView.backgroundColor = [UIColor whiteColor];
    
    JShopCarShopModel *model = _dataArray[section];
    headView.title = model.VendorName;
    headView.select = model.isSel;
    headView.editing = model.isEdit;
    headView.JSelectBlock = ^(BOOL select) {
        model.isSel = select;
        if (select) {
            
            for (JShopCarDetailModel *good in model.goodsArray) {
                good.isSel = YES;
                if (![self.selectedArray containsObject:good]) {
                    
                    [self.selectedArray addObject:good];
                }
            }
            
        } else {
            for (JShopCarDetailModel *good in model.goodsArray) {
                good.isSel = NO;
                if ([self.selectedArray containsObject:good]) {
                    
                    [self.selectedArray removeObject:good];
                }
            }
        }
        
        [self verityAllSelectState];
        
        [tableView reloadData];
        [self countPrice];
    };
    
    headView.JEditBlock = ^(BOOL select) {
        model.isEdit = select;
        if (select) {
            
            for (JShopCarDetailModel *good in model.goodsArray) {
                good.isEdit = YES;
            }
            [tableView reloadData];
            
        } else {
            [_changeArray removeAllObjects];
            for (JShopCarDetailModel *good in model.goodsArray) {
                good.isEdit = NO;
                
                // 更换商品数量
//                JUpdateProductModel *model = [JUpdateProductModel new];
//                model.Id = good.Id;
//                model.Qty = good.Quantity;
//                model.CustomerEnteredPrice = good.CustomerEnteredPrice;
                NSDictionary *dic = @{@"Id":[NSString stringWithFormat:@"%ld",(long)good.Id],
                                      @"Qty":[NSString stringWithFormat:@"%ld",(long)good.Quantity],
                                      @"CustomerEnteredPrice":[NSString stringWithFormat:@"%ld",(long)good.CustomerEnteredPrice]};
                
//                NSDictionary *dic = model.mj_keyValues;
                
                [_changeArray addObject:dic];
            }
            
            
            // 更新商品数据到数据库
            [self UpdateProductNumber];
        }
//        [tableView reloadData];

    };
    return headView;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor whiteColor];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JShopCarTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JShopCarTableCell class])];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    JShopCarShopModel *shopModel = self.dataArray[indexPath.section];
    JShopCarDetailModel *model = [shopModel.goodsArray objectAtIndex:indexPath.row];
    __block typeof(cell)wsCell = cell;
    cell.isEdit = model.isEdit;
    [cell numberAddWithBlock:^(NSInteger number) {
        wsCell.dwqNumber = number;
        model.Quantity = number;
        [self countPrice];
//        [shopModel.goodsArray replaceObjectAtIndex:indexPath.row withObject:model];
//        if ([self.selectedArray containsObject:model]) {
//            [self.selectedArray removeObject:model];
//            [self.selectedArray addObject:model];
//            [self countPrice];
//        }
    }];
    
    [cell numberCutWithBlock:^(NSInteger number) {
        
        wsCell.dwqNumber = number;
        model.Quantity = number;
        [self countPrice];
//        [shopModel.goodsArray replaceObjectAtIndex:indexPath.row withObject:model];
//        
//        //判断已选择数组里有无该对象,有就删除  重新添加
//        if ([self.selectedArray containsObject:model]) {
//            [self.selectedArray removeObject:model];
//            [self.selectedArray addObject:model];
//            [self countPrice];
//        }
    }];
    
    [cell cellSelectedWithBlock:^(BOOL select) {
        
        model.isSel = select;
        if (select) {
            [self.selectedArray addObject:model];
        } else {
            [self.selectedArray removeObject:model];
        }
        
        [self verityAllSelectState];
        [self verityGroupSelectState:indexPath.section];
        
        [self countPrice];
    }];
    
    [cell delCellBtnWithBlock:^{
        [self deleteCell:indexPath];
    }];
    
    cell.imgBlock = ^(JProductDetailModel *product) {
        JProductDetailController *dvc = [[JProductDetailController alloc]init];
        dvc.hidesBottomBarWhenPushed = YES;
        dvc.productModel = product;
        dvc.inputType = 2; // 表示购物车进入商品详情
        [self.navigationController pushViewController:dvc animated:YES];
    };
    
    [cell reloadDataWithModel:model];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self deleteCell:indexPath];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return HeaderViewHeight;
}


- (void)reloadTable {
    [self.myTableView reloadData];
}

#pragma mark -- 自定义底部视图
- (void)setupCustomBottomView {
    
    UIView *backgroundView = [[UIView alloc]init];
    backgroundView.backgroundColor = kRGBColor(245, 245, 245);
    backgroundView.tag = TAG_CartEmptyView + 1;
    [self.contentView addSubview:backgroundView];
    backgroundView.frame = CGRectMake(0, CGRectGetHeight(self.contentView.frame) -  2 *49, kWindowW , 49);
    
    
    UIView *lineView = [[UIView alloc]init];
    lineView.frame = CGRectMake(0, 0, kWindowW, 1);
    lineView.backgroundColor = [UIColor lightGrayColor];
    [backgroundView addSubview:lineView];
    
    //全选按钮
    UIButton *selectAll = [UIButton buttonWithType:UIButtonTypeCustom];
    selectAll.titleLabel.font = [UIFont systemFontOfSize:16];
    selectAll.frame = CGRectMake(10, 5, 80, 49 - 10);
    [selectAll setTitle:@" 全选" forState:UIControlStateNormal];
    [selectAll setImage:[UIImage imageNamed:SelBtnImg_N] forState:UIControlStateNormal];
    [selectAll setImage:[UIImage imageNamed:SelBtnImg_H] forState:UIControlStateSelected];
    [selectAll setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [selectAll addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:selectAll];
    self.allSellectedButton = selectAll;
    
    //结算按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake(kWindowW - 100, 0, 100, 49);
    [btn setTitle:@"去结算(0)" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(goToPayButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:btn];
    self.allCountButton = btn;
    //合计
    UILabel *label = [[UILabel alloc]init];
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor redColor];
    [backgroundView addSubview:label];
    label.textAlignment = NSTextAlignmentRight;
    label.attributedText = [self DWQSetString:@"¥0.00"];
    CGFloat maxWidth = kWindowW - selectAll.bounds.size.width - btn.bounds.size.width - 30;
    label.frame = CGRectMake(kWindowW - maxWidth - 10 - 5 - btn.bounds.size.width, 0, maxWidth - 10, 49);
    self.totlePriceLabel = label;
}

#pragma mark -- 购物车为空时的默认视图
- (void)changeView {
    if (self.dataArray.count > 0) {
        UIView *view = [self.view viewWithTag:TAG_CartEmptyView];
        if (view != nil) {
            [view removeFromSuperview];
        }
        
        [self setupCartView];
    } else {
        UIView *bottomView = [self.view viewWithTag:TAG_CartEmptyView + 1];
        [bottomView removeFromSuperview];
        
        [self.myTableView removeFromSuperview];
        self.myTableView = nil;
        [self setupCartEmptyView];
    }
}

- (NSMutableAttributedString*)DWQSetString:(NSString*)string {
    
    NSString *text = [NSString stringWithFormat:@"合计:%@",string];
    NSMutableAttributedString *DWQString = [[NSMutableAttributedString alloc]initWithString:text];
    NSRange rang = [text rangeOfString:@"合计:"];
    [DWQString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:rang];
    [DWQString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:rang];
    return DWQString;
}

#pragma mark - loadRequest
/// 加载购物车 商品列表
- (void)loadData {
    
    [SVProgressHUD show];
    NSString *url = @"ShoppingCartItems/GetShoppingCartItems";
    
    [self.dataArray removeAllObjects];
    [JNetManager GetShopCarProductListUrl:url
                               productIDs:nil
                                   userID:[GLOBALDATA userInfoModel]
                                  success:^(id response) {
                                      if (response) {
                                          NSArray *arr = (NSArray *)response;
                                          if (arr.count > 0) {
                                              self.dataArray = [NSMutableArray arrayWithArray:arr];
                                              [self setYouLikeStrAttribute:arr];
                                          }
                                          [SVProgressHUD dismiss];
                                          [self changeView];
                                      }
                                  } failure:^(NSString *err) {
                                      [SVProgressHUD dismiss];
                                      [SVProgressHUD showErrorWithStatus:err];
                                  }];
    
}

/// 配置猜你喜欢参数；
- (void)setYouLikeStrAttribute:(NSArray *)arr
{
    self.youLikeStr = [NSMutableString string];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i< arr.count; i++) {
            JShopCarShopModel *model = arr[i];
            if (i == arr.count - 1) {
                [self.youLikeStr appendString:model.Ids];
            }
            else{
                [self.youLikeStr appendString:model.Ids];
                [self.youLikeStr appendString:@","];
            }
        }
        
        self.youLikeModel.Ids = self.youLikeStr ;
        [self LoadYouLikeRequest];
        
    });
}

/// 更新商品数量
- (void)UpdateProductNumber
{
    if (self.changeArray.count == 0) {
        return;
    }
    NSArray *array = [NSArray arrayWithArray:self.changeArray];
    NSString *url = @"ShoppingCartItems/UpdateShoppingCartItems";
    
    [JNetManager UpdateShopCarProductListUrl:url
                                  productArr:array
                                     success:^(id response) {
                                         if ([response integerValue] == 1) {
                                             [self loadData];
//                                             [self.myTableView reloadData];
                                         }
                                     }
                                     failure:^(NSString *err) {
                                         [SVProgressHUD showErrorWithStatus:err];
                                     }];
}

/// 删除商品
- (void)DelectProductID:(NSInteger)productId
{
    
    NSString *url = @"ShoppingCartItems/DeleteShoppingCartItem";
    
    [JNetManager DelectShopCarProductUrl:url
                               productID:productId
                                 success:^(id response) {
                                     if ([response integerValue] == 1) {
                                         [self loadData];
                                         //                                             [self.myTableView reloadData];
                                     }
                                 }
                                 failure:^(NSString *err) {
                                     [SVProgressHUD showErrorWithStatus:err];

                                 }];

}

/// 加载猜你喜欢信息
- (void)LoadYouLikeRequest
{
    NSString *url = @"Products/AssociatedProductList";
    //    Products/AssociatedProductList
    [JNetManager LoadYouLikeCommentUrl:url
                        productIDModel:self.youLikeModel
                               success:^(id response) {
                                   if (response) {
                                       NSArray *arr = response;
                                       if (arr.count > 0) {
                                           
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               [self.myTableView reloadData];
                                               _foorerView.hidden = NO;
                                               _foorerView.frame = CGRectMake(0, 0, kWindowH, [_foorerView getViewHeight]);

                                           });
                                       }
                                       else
                                       {
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               _foorerView.hidden = YES;
                                               _foorerView.frame = CGRectMake(0, 0, kWindowH, 0);
                                               
                                           });
                                       }

                                   }
                               }
                               failure:^(NSString *err) {
                                   
                                   [SVProgressHUD showErrorWithStatus:@"猜你喜欢加载失败"];
                               }
     ];
}

/// 加载收货地址 判断是否有收货地址
- (void)LoadCustomerAddressRequest
{
    NSString *url = @"Address/GetAddress";
    
    [JNetManager CustomerAddressRequestUrl:url
                                CustumerID:[GLOBALDATA userInfoModel].CustomerId
                                   success:^(id response) {
                                       if (response == [NSNull null])
                                       {
                                           UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有设置收货地址，请点击这里设置" preferredStyle:1];
                                           UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                               JCustomerAddressController *addressvc = [[JCustomerAddressController alloc]init];
                                               addressvc.hidesBottomBarWhenPushed = YES;
                                               [self.navigationController pushViewController:addressvc animated:YES];
                                               
                                               
                                           }];
                                           
                                           UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                                           
                                           [alert addAction:okAction];
                                           [alert addAction:cancel];
                                           [self presentViewController:alert animated:YES completion:nil];
                                       }
                                       else
                                       {
                                           JPayController *payVC = [[JPayController alloc]init];
                                           payVC.productIDs = _productSelIds;
                                           payVC.hidesBottomBarWhenPushed = YES;
                                           [self.navigationController pushViewController:payVC animated:YES];
                                       }
                                   } failure:^(NSString *err) {
                                       [SVProgressHUD showErrorWithStatus:err];

                                   }];
}

#pragma mark -
/*
 *
 *  计算已选中商品金额
 */
-(void)countPrice {
    [_productSelIds setString:@""];
    double totlePrice = 0.0;
    NSInteger num = 0;
    for (int i = 0;i< self.selectedArray.count; i++) {
        JShopCarDetailModel *model = self.selectedArray[i];
        totlePrice += model.CustomerEnteredPrice * model.Quantity;
        num += model.Quantity;
        
        if (i == self.selectedArray.count-1) {
            [_productSelIds appendString:[NSString stringWithFormat:@"%ld",(long)model.Id]];
        }
        else
        {
            [_productSelIds appendString:[NSString stringWithFormat:@"%ld,",(long)model.Id]];
        }
        
    }
    NSString *string = [NSString stringWithFormat:@"￥%.2f",totlePrice];
    
    [self.allCountButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",(long)num] forState:UIControlStateNormal];
    self.totlePriceLabel.attributedText = [self DWQSetString:string];
}

- (void)deleteCell:(NSIndexPath *)indexPath
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除该商品?删除后无法恢复!" preferredStyle:1];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
                JShopCarShopModel *shop = [self.dataArray objectAtIndex:indexPath.section];
                JProductDetailModel *model = [shop.goodsArray objectAtIndex:indexPath.row];
//
//                [shop.goodsArray removeObjectAtIndex:indexPath.row];
//                //    删除
//                [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//    
//                if (shop.goodsArray.count == 0) {
//                    [self.dataArray removeObjectAtIndex:indexPath.section];
//                }
//    
//                //判断删除的商品是否已选择
//                if ([self.selectedArray containsObject:model]) {
//                    //从已选中删除,重新计算价格
//                    [self.selectedArray removeObject:model];
//                    [self countPrice];
//                }
//    
//                NSInteger count = 0;
//                for (JShopCarShopModel *shop in self.dataArray) {
//                    count += shop.goodsArray.count;
//                }
//    
//                if (self.selectedArray.count == count) {
//                    _allSellectedButton.selected = YES;
//                } else {
//                    _allSellectedButton.selected = NO;
//                }
//    
//                if (count == 0) {
//                    [self changeView];
//                }
        [self DelectProductID:model.Id];
        
        //
        //如果删除的时候数据紊乱,可延迟0.5s刷新一下
//        [self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.5];
        
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alert addAction:okAction];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];

}

#pragma mark --- 全选按钮点击事件
- (void)selectAllBtnClick:(UIButton*)button {
    button.selected = !button.selected;
    
    //点击全选时,把之前已选择的全部删除
    for (JShopCarDetailModel *model in self.selectedArray) {
        model.isSel = NO;
    }
    
    [self.selectedArray removeAllObjects];
    
    if (button.selected) {
        
        for (JShopCarShopModel *shop in self.dataArray) {
            shop.isSel = YES;
            for (JShopCarDetailModel *model in shop.goodsArray) {
                model.isSel = YES;
                [self.selectedArray addObject:model];
            }
        }
        
    } else {
        for (JShopCarShopModel *shop in self.dataArray) {
            shop.isSel = NO;
        }
    }
    
    [self.myTableView reloadData];
    [self countPrice];
    
}

#pragma mark --- 确认选择,提交订单按钮点击事件
- (void)goToPayButtonClick:(UIButton*)button {
    if (self.selectedArray.count > 0) {
        
        /// 支付
        [self LoadCustomerAddressRequest];
    
    } else {
        [SVProgressHUD showErrorWithStatus:@"请选择要结算的商品"];
    }
    
}

- (void)verityGroupSelectState:(NSInteger)section {
    
    // 判断某个区的商品是否全选
    JShopCarShopModel *tempShop = self.dataArray[section];
    // 是否全选标示符
    BOOL isShopAllSelect = YES;
    for (JShopCarDetailModel *model in tempShop.goodsArray) {
        // 当有一个为NO的是时候,将标示符置为NO,并跳出循环
        if (model.isSel == NO) {
            isShopAllSelect = NO;
            break;
        }
    }
    
    JShopCarHeaderView *header = (JShopCarHeaderView *)[self.myTableView headerViewForSection:section];
    header.select = isShopAllSelect;
    tempShop.isSel = isShopAllSelect;
}

- (void)verityAllSelectState {
    
    NSInteger count = 0;
    for (JShopCarShopModel *shop in self.dataArray) {
        count += shop.CarItems.count;
    }
    
    if (self.selectedArray.count == count) {
        _allSellectedButton.selected = YES;
    } else {
        _allSellectedButton.selected = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
